import spacy

nlp = spacy.load('zh')


def check_if_from_same_sentence(str1, str2):
    doc1 = nlp(str1)
    doc2 = nlp(str2)

    entities1 = set([(ent.text, ent.label_) for ent in doc1.ents])
    entities2 = set([(ent.text, ent.label_) for ent in doc2.ents])

    common_entities = entities1.intersection(entities2)

    if common_entities:
        return True, "Common entities found: " + ", ".join([ent[0] for ent in common_entities])
    else:
        return False, "No common entities found."

    # 示例用法


str1 = "Apple released the iPhone 14 today."
str2 = "The new iPhone 14 has a better camera."
is_from_same_sentence, message = check_if_from_same_sentence(str1, str2)
print(is_from_same_sentence, message)  # 输出: True, Common entities found: iPhone 14